<template>
  <div id="app">
    <ul class="new-list" v-loading="isLoading">
      <li class="new-item" v-for="(news,index) in newsList" :key="index">
        <div class="left">
          <span class="title">{{ news.title }}</span>
          <div>
            <span class="info date">{{ news.source }}</span>
            <span class="date">{{ news.time }}</span>
          </div>
        </div>
        <div class="right">
          <img :src="news.img" alt="">
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name: "App",
  data() {
    return {
      isLoading: true,
      newsList: [],
    }
  },
  methods: {
    async getNewListApi() {
      const {data: {data: result}} = await axios({url: "http://hmajax.itheima.net/api/news", method: "GET"})
      this.newsList = result
      this.isLoading = false
    }
  },
  directives: {
    loading: {
      inserted(el, binding) {
        if (binding.value) {
          el.classList.add("loading")
        } else {
          el.classList.remove("loading")
        }
      },
      update(el, binding) {
        if (binding.value) {
          el.classList.add("loading")
        } else {
          el.classList.remove("loading")
        }
      },
    }
  },
  created() {
    setTimeout(()=>{
      this.getNewListApi()
    },2000)
  },

}
</script>
<style lang="less">
.new-list {
  position: relative;
  width: 1000px;
  height: 1124px;
  background-color: #ececec;
  margin: 100px auto;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  .new-item {
    width: 100%;
    height: 200px;
    padding: 5px;
    background-color: #ffffff;
    display: flex;

    .left {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 10px 0;

      .title {
        font-size: 18px;
        font-weight: 600;
      }

      .date {
        font-size: 15px;
        color: #9b9696;
        margin: 0 10px;
      }
    }

    .right {
      width: 300px;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
      }

    }
  }
}
.loading:before {
  content: "正在加载中.....";
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #c5c5c5;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  color: #ffffff;
}
</style>
